package com.goldman.oss.common.internal.processor;


import com.goldman.oss.common.internal.pojo.DownloadFileInfo;
import com.goldman.oss.common.internal.processor.context.ProcessorContext;
import com.goldman.oss.common.platform.FileStorage;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.io.InputStream;
import java.util.function.Consumer;


@Accessors(chain = true)
@Data
@Slf4j
public class DownloadProcessor {

    private String objectKey;
    private Long expirationTimeInSeconds;

    private String businessCode;


    public String generateTemporaryUrl() {
        DownloadFileInfo downloadFileInfo = new DownloadFileInfo();
        BeanUtils.copyProperties(this, downloadFileInfo);
        FileStorage fileStorage = ProcessorContext.getFileStorage(businessCode);
        return fileStorage.generateTemporaryUrl(downloadFileInfo);
    }

    public String generatePublicUrl() {
        FileStorage fileStorage = ProcessorContext.getFileStorage(businessCode);
        return fileStorage.generatePublicUrl(this.objectKey);
    }

    public void download(Consumer<InputStream> consumer) {
        DownloadFileInfo downloadFileInfo = new DownloadFileInfo();
        BeanUtils.copyProperties(this, downloadFileInfo);
        FileStorage fileStorage = ProcessorContext.getFileStorage(businessCode);
        fileStorage.download(downloadFileInfo, consumer);
    }

    public InputStream download() {
        DownloadFileInfo downloadFileInfo = new DownloadFileInfo();
        BeanUtils.copyProperties(this, downloadFileInfo);
        FileStorage fileStorage = ProcessorContext.getFileStorage(businessCode);
        return fileStorage.download(downloadFileInfo);
    }

}
